package com.tnxst.phexamination.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.tnxst.phexamination.R;
import com.tnxst.phexamination.dto.SignDTO;
import com.tnxst.phexamination.dto.TCMHealthDTO;
import com.tnxst.phexamination.fragment.SignFragment;
import com.tnxst.phexamination.utils.ReflectUtils;
import com.tnxst.phexamination.utils.StringUtils;

import java.util.List;

public class RecordTcmHealthElderlyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;

    private List<String> str;

    private TCMHealthDTO tcmHealthDTO;



    public RecordTcmHealthElderlyAdapter(List<String> str,TCMHealthDTO tcmHealthDTO) {
        this.str = str;
        this.tcmHealthDTO = tcmHealthDTO;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view =  LayoutInflater.from(context).inflate(R.layout.record_tcm_health_elderly_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ViewHolder holders = (ViewHolder) holder;
        holders.textView.setText(str.get(position));
        int question = position + 1;
        String filedName = question > 9 ? "A" + question : "A0" + question;
        Object answer = "";
        if (StringUtils.isNotNull(answer = ReflectUtils.getFiledByFiledName(tcmHealthDTO, filedName))){
            switch (answer.toString()){
                case "1":
                    holders.radioGroup.check(R.id.radioButton289);
                    break;
                case "2":
                    holders.radioGroup.check(R.id.radioButton290);
                    break;
                case "3":
                    holders.radioGroup.check(R.id.radioButton291);
                    break;
                case "4":
                    holders.radioGroup.check(R.id.radioButton292);
                    break;
                case "5":
                    holders.radioGroup.check(R.id.radioButton293);
                    break;
            }
        }else {
            holders.radioGroup.clearCheck();
        }


        //设置保存答案
        holders.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                String answer = "";
                switch (checkedId){
                    case R.id.radioButton289:
                        answer = "1";
                        break;
                    case R.id.radioButton290:
                        answer = "2";
                        break;
                    case R.id.radioButton291:
                        answer = "3";
                        break;
                    case R.id.radioButton292:
                        answer = "4";
                        break;
                    case R.id.radioButton293:
                        answer = "5";
                        break;
                }
                if (StringUtils.isNotNull(answer))
                    ReflectUtils.setFiledByFiledName(tcmHealthDTO, filedName, answer);

            }
        });

    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public View view;

        public TextView textView;

        public RadioGroup radioGroup;

        public RadioButton radioButton1;

        public RadioButton radioButton2;

        public RadioButton radioButton3;

        public RadioButton radioButton4;

        public RadioButton radioButton5;

        public ViewHolder(View view) {
            super(view);

            this.view = view;

            textView = view.findViewById(R.id.textView);

            radioGroup = view.findViewById(R.id.radioGroup1);

            radioButton1 = view.findViewById(R.id.radioButton289);

            radioButton2 = view.findViewById(R.id.radioButton290);

            radioButton3 = view.findViewById(R.id.radioButton291);

            radioButton4 = view.findViewById(R.id.radioButton292);

            radioButton5 = view.findViewById(R.id.radioButton293);
        }
    }



    @Override
    public int getItemCount() {
        return str.size();
    }

}
